<?php

namespace app\model;

use Illuminate\Database\Eloquent\SoftDeletes;
use support\Model;

class UserModel extends Model
{

    protected $table = 'user';
    protected $primaryKey = 'id';
    public $timestamps = true;
    protected $dateFormat = 'U';
    const CREATED_AT = 'create_time';
    const UPDATED_AT = 'update_time';
    use SoftDeletes;
    const DELETED_AT = 'delete_time';

    protected $fillable = ['nickname', 'email', 'mobile', 'password', 'invite'];

    const NORMAL = 1;
    const FORBID = 2;
    const LOCK = 3;
    const WRITE_OFF = 4;

    public function asset() {
        return $this->hasOne(UserAssetModel::class, 'user_id', 'id');
    }

    // 创建信息
    public static function createInfo($data) {
        return self::create($data);
    }

    // 编辑信息
    public static function updateInfo($where, $data) {
        return self::where($where)->update($data);
    }

    // 删除信息
    public static function deleteInfo($data) {
        $model = '';
        switch (true) {
            case is_numeric($data) :
                $model = self::where('id', '=', $data);
                break;
            case is_array($data) :
                $assist = 0;
                foreach ($data as $value) {

                    switch (true) {
                        case is_numeric($value) :
                            if ($assist == 2) throw new \Exception('删除失败! 删除条件有误!');
                            $assist = 1;
                            break;
                        case is_array($value) :
                            if ($assist == 1) throw new \Exception('删除失败! 删除条件有误!');
                            $assist = 2;
                            break;
                        default :
                            throw new \Exception('删除失败! 删除条件有误!');
                            break;
                    }
                }
                if ($assist == 1) $model = self::whereIn('id', $data);
                if ($assist == 2) $model = self::where($data);
                break;
            default :
                $arr = explode(',', $data);
                if (count($arr) <= 1) throw new \Exception('删除失败! 删除条件有误!');
                foreach ($arr as $item) {
                    if (!is_numeric($item)) throw new \Exception('删除失败! 删除条件有误!');
                }
                $model = self::whereIn('id', $arr);
                break;
        }
        return $model->delete();
    }

    // 查询列表
    public static function getList($where=[], $field='*', $order=[], $paging=1, $page=1, $pagesize=10, $with=[]) {
        $with = self::checkWith($with);
        $sql = self::where($where)->with($with)->select($field)->orderBy($order);
        if ($paging) {
            $sql = $sql->forPage(1, 15);
        }
        return $sql->get();
    }

    // 统计数量
    public static function getCount($where) {
        return self::where($where)->count('id');
    }
    // 查询信息
    public static function getInfoById($id, $field = '*', $with = []) {
        $with = self::checkWith($with);
        return self::with($with)->select($field)->find($id);
    }

    // 查询信息
    public static function getInfoByWhere($where, $field = '*', $with = []) {
        $with = self::checkWith($with);
        var_dump('$where', $where);
        var_dump(self::with($with)->where($where)->select($field)->toSql());
        return  self::with($with)->where($where)->select($field)->first();
    }

    // 检查关联数据
    public static function checkWith($with) {
        if (!empty($with)) {
            foreach ($with as $key => $item) {
                if(!in_array($item, ['111'])) unset($with[$key]);
            }
            $with = array_values($with);
        }
        return $with;
    }

}